package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.FormListInProcessDTO;
import com.xbongbong.paas.pojo.dto.MobileMenuListDTO;
import com.xbongbong.paas.pojo.dto.MobileProcessFormListDTO;
import com.xbongbong.paas.pojo.vo.FormListInProcessVO;
import com.xbongbong.paas.pojo.vo.MobileMenuListVO;
import com.xbongbong.paas.pojo.vo.MobileProcessFormListVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.PaasMenuService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.app.pojo.dto.AppMenuFormIdDTO;
import com.xbongbong.pro.app.pojo.vo.AppMenuFormIdVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.menu.pojo.dto.HomeMenuSetListDTO;
import com.xbongbong.pro.menu.pojo.dto.MenuChangeStateDTO;
import com.xbongbong.pro.menu.pojo.dto.MenuNewListDTO;
import com.xbongbong.pro.menu.pojo.dto.MenuSetDTO;
import com.xbongbong.pro.menu.pojo.vo.HomeMenuSetListVO;
import com.xbongbong.pro.menu.pojo.vo.MenuChangeStateVO;
import com.xbongbong.pro.menu.pojo.vo.MenuNewListVO;
import com.xbongbong.pro.menu.pojo.vo.MenuSetVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author: wufeng
 * @date: 2018/8/8 10:57
 * @desrcption:
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/menu")
public class MobilePaasMenuController {

    @Resource
    private PaasMenuService paasMenuService;
    @Resource
    private PaasRedisHelper paasRedisHelper;
    /**
     * 菜单列表
     * @param mobileMenuListDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid MobileMenuListDTO mobileMenuListDTO, BindingResult br) throws Exception {
        XbbResponse<MobileMenuListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MobileMenuListVO mobileMenuListVO = this.paasMenuService.mobileMenuList(mobileMenuListDTO);
                response = new XbbResponse<>(mobileMenuListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 带应用的菜单列表
     */
    @RequestMapping(value = "/home/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid MenuNewListDTO menuListDTO, BindingResult br) throws Exception {
        XbbResponse<MenuNewListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MenuNewListVO menuListVO = paasMenuService.list(menuListDTO);
                response = new XbbResponse<>(menuListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 查询某个应用中的流程列表，即用于发起流程显示
     * @param processFormListDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/process/form/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String mobileProcessFormList(@RequestBody @Valid MobileProcessFormListDTO processFormListDTO, BindingResult br) throws Exception {
        XbbResponse<MobileProcessFormListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MobileProcessFormListVO processFormListVO = this.paasMenuService.getMobileProcessFormList(processFormListDTO);
                response = new XbbResponse<>(processFormListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 流程菜单
     * @param formListInProcessDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list/in/process", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String formListInProcess(@RequestBody @Valid FormListInProcessDTO formListInProcessDTO, BindingResult br) throws Exception {
        XbbResponse<FormListInProcessVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormListInProcessVO formListInProcessVO = this.paasMenuService.formListInProcess(formListInProcessDTO);
                response = new XbbResponse<>(formListInProcessVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 首页-菜单设置-菜单列表
     * @param homeMenuSetListDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/home/set/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String homeMenuSetList(@RequestBody @Valid HomeMenuSetListDTO homeMenuSetListDTO, BindingResult br) throws Exception{
        XbbResponse<HomeMenuSetListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                HomeMenuSetListVO homeMenuSetListVO = paasMenuService.homeMenuSetList(homeMenuSetListDTO);
                response = new XbbResponse<>(homeMenuSetListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 个人快捷新建和常用表单设置
     */
    @RequestMapping(value = "/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String set(@RequestBody @Valid MenuSetDTO menuSetDTO, BindingResult br) throws Exception {
        XbbResponse<MenuSetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MenuSetVO menuSetVO = paasMenuService.set(menuSetDTO);
                response = new XbbResponse<>(menuSetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取AppMenuFoemId等参数
     */
    @RequestMapping(value = "/getAppFormMenuId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getAppFormMenuId(@RequestBody @Valid AppMenuFormIdDTO appMenuFormIdDTO, BindingResult br) throws Exception {
        XbbResponse<AppMenuFormIdVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                AppMenuFormIdVO appMenuFormIdVO = paasMenuService.getAppFormMenuId(appMenuFormIdDTO);
                response = new XbbResponse<>(appMenuFormIdVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 应用&菜单 开启/关闭接口
     * 可以同时支持应用和菜单的开启关闭操作，
     *
     */
    @RequestMapping(value = "/changeState", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String changeState(@RequestBody @Valid MenuChangeStateDTO menuChangeStateDTO, BindingResult br) throws Exception {
        XbbResponse<MenuChangeStateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(menuChangeStateDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(menuChangeStateDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                MenuChangeStateVO menuChangeStateVO = paasMenuService.changeState(menuChangeStateDTO);
                response = new XbbResponse<>(menuChangeStateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
